package com.example.mall.auth.svc.config;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForStateless;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.router.SaRouterStaff;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.example.mall.common.constants.CodeEnum;
import com.example.mall.common.exception.BizException;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author Mickey
 * @since 2022/4/10
 **/
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token的路由拦截器
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {
//            SaRouter.match("/**", this::handle);
            // 登录认证 -- 拦截所有路由，并排除/user/doLogin 用于开放登录

//            SaRouter
//                    .notMatch("/webjars/**", "/swagger-resources", "/v2/api-docs/**", "/doc.html/**")
//                    .match("/**", "/auth/doLogin", r -> StpUtil.checkLogin());
//            // 根据路由划分模块，不同模块不同鉴权
//            SaRouter.match("/user/**", r -> StpUtil.checkPermission("user"));
//            SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
//            SaRouter.match("/goods/**", r -> StpUtil.checkPermission("goods"));
//            SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));
//            SaRouter.match("/notice/**", r -> StpUtil.checkPermission("notice"));
//            SaRouter.match("/comment/**", r -> StpUtil.checkPermission("comment"));
        }));
//                .addPathPatterns("/**");
//                .excludePathPatterns("/auth/doLogin")
//                .excludePathPatterns("/webjars/**")
//                .excludePathPatterns("/swagger-resources")
//                .excludePathPatterns("/v2/api-docs/**")
//                .excludePathPatterns("/doc.html/**");
    }

    public void handle(SaRouterStaff saRouterStaff) {

//        List<Router> routers = new ArrayList<>();
//        routers.add(new Router("/doc.html/**", r -> System.out.println("w")));
//        routers.add(new Router("/webjars/**", r -> System.out.println("w")));
//        routers.add(new Router("/swagger-resources", r -> System.out.println("w")));
//        routers.add(new Router("/v2/api-docs/**", r -> System.out.println("w")));
//        routers.add(new Router("/**", r -> StpUtil.checkLogin()));
//        routers.add(new Router("/user/**", r -> StpUtil.checkPermission("user")));
//
//        for (Router router : routers) {
//            if (SaRouter.isMatchCurrURI(router.path)) {
//                router.getHandle().accept(saRouterStaff);
//                break;
//            }
//        }
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForStateless();
    }

    @Data
    public static class Router {

        private String path;
        private Consumer<SaRouterStaff> handle;

        public Router(String path, Consumer<SaRouterStaff> handle) {
            this.path = path;
            this.handle = handle;
        }
    }

}
